package com.comdo.newup_loan.module.bankInfo;

import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.comdo.newup_loan.R;
import com.comdo.newup_loan.bank.BankUtils;
import com.comdo.newup_loan.bank.bank.BankApiManager;
import com.comdo.newup_loan.base.BaseActivity;
import com.comdo.newup_loan.base.BasePresenter;
import com.comdo.newup_loan.comom.Config;
import com.comdo.newup_loan.comom.ProgressDialog;
import com.comdo.newup_loan.http.RetrofitHelper;
import com.comdo.newup_loan.util.TitleMenuUtil;

import butterknife.BindView;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;
import okhttp3.FormBody;
import retrofit2.Response;

/**
 * Created by liyuan on 2017/11/3.
 */

public class BankInfoActivity extends BaseActivity {
    @BindView(R.id.tv_userno)
    TextView userNoTextView;
    @BindView(R.id.tv_bankno)
    TextView bankNoTextView;
    @BindView(R.id.tv_bankname)
    TextView bankNameTextView;
    @BindView(R.id.tv_phone)
    TextView phoneTextView;
    @BindView(R.id.tv_username)
    TextView userNameTextView;
    @BindView(R.id.ll_content)
    LinearLayout mContentLayout;
    @BindView(R.id.ll_to_register)
    LinearLayout mToRegLayout;

    @Override
    protected int getContentViewLayoutID() {
        return R.layout.activity_bank_info;
    }

    @Override
    protected void init() {
        super.init();
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        new TitleMenuUtil(BankInfoActivity.this, "存管银行开户信息", false);
        if (Config.userInfo.getRealNameCheck() == 0) {
            mToRegLayout.setVisibility(View.VISIBLE);
            mContentLayout.setVisibility(View.GONE);
        } else {
            mToRegLayout.setVisibility(View.GONE);
            mContentLayout.setVisibility(View.VISIBLE);
            userNoTextView.setText(Config.userInfo.getPlatformUserNo());
            bankNoTextView.setText(Config.userInfo.getBankAccount());
            bankNameTextView.setText(Config.userInfo.getBankName());
            phoneTextView.setText(Config.userInfo.getPhone());
            userNameTextView.setText(Config.userInfo.getAccount());
        }
        mToRegLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (Config.loginInfo.getActivateAccount() == 0) {
                    CompositeDisposable mCompositeDisposable = new CompositeDisposable();
                    ProgressDialog.show(BankInfoActivity.this, false, "正在请求新网银行...");
                    FormBody body = BankApiManager.getIstance().personalTieCardRegister(BankInfoActivity.this, Config.loginInfo.getPlatformUserNo());
                    mCompositeDisposable.add(RetrofitHelper.createMyBankApi().getWebUrl(body)
                            .subscribeOn(Schedulers.io())
                            .observeOn(AndroidSchedulers.mainThread())
                            .subscribe(new Consumer<Response<String>>() {
                                @Override
                                public void accept(Response<String> Result) throws Exception {

                                    ProgressDialog.cancle();
                                    BankInfoActivity.this.finish();
                                    BankUtils.toWebView(Result.headers().get("Location"), BankInfoActivity.this);
                                }
                            }, new Consumer<Throwable>() {
                                @Override
                                public void accept(Throwable throwable) throws Exception {
                                    ;
                                    ProgressDialog.cancle();
                                    Toast.makeText(BankInfoActivity.this, "请求失败", Toast.LENGTH_SHORT).show();
                                }
                            }));
                } else {
                    CompositeDisposable mCompositeDisposable = new CompositeDisposable();
                    ProgressDialog.show(BankInfoActivity.this, false, "正在请求新网银行...");
                    FormBody body = BankApiManager.getIstance().activityUser(BankInfoActivity.this, Config.loginInfo.getPlatformUserNo());
                    mCompositeDisposable.add(RetrofitHelper.createMyBankApi().getWebUrl(body)
                            .subscribeOn(Schedulers.io())
                            .observeOn(AndroidSchedulers.mainThread())
                            .subscribe(new Consumer<Response<String>>() {
                                @Override
                                public void accept(Response<String> Result) throws Exception {
                                    BankInfoActivity.this.finish();
                                    BankUtils.toWebView(Result.headers().get("Location"), BankInfoActivity.this);

                                    ProgressDialog.cancle();
                                }
                            }, new Consumer<Throwable>() {
                                @Override
                                public void accept(Throwable throwable) throws Exception {
                                    Toast.makeText(BankInfoActivity.this, "请求失败", Toast.LENGTH_SHORT).show();
                                    ProgressDialog.cancle();
                                }
                            }));
                }
            }
        });
    }

    @Override
    public BasePresenter getmPresenter() {
        return null;
    }

    @Override
    public void presenterSetView() {

    }
}
